<?php

/**
 * uniBoard ACP Edit User Ban Form
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_user_ban{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
			
		// Create API
		$this -> api = new api_ban( $appCore, $appCore -> _REQ( 'ban'));
		
		// Data not loaded?
		if ( !$this -> api -> data_loaded || $this -> api -> user_id == 0)
		{
			// Set Error
			$appCore -> output -> error( $appCore -> lang['acp_ban_not_found'], $appCore -> lang['acp_ban_users']);
			
			// Hop back to Bans List
			new action_ban_users( $appCore);
		}
		else if ( $appCore -> _POST( 'update') == 1)
		{
			// Set Api Reasons thingie
			$this -> api -> setBanReasonAdmin( $appCore -> _POST('ban_reason_admin'));
			$this -> api -> setBanReasonUser( $appCore -> _POST('ban_reason_user'));
			
			// Change ban lenght?
			if ( $appCore -> _POST( 'ban_lenght') > -1 || strlen( $appCore -> _POST( 'ban_lenght_custom')) > 0)
			{
				// Change Ban Lenght
				$this -> api -> setBanLenght( ( 3600 * $appCore -> _POST( 'ban_lenght')), $appCore -> _POST( 'ban_lenght_custom'));
			}
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['edit_ban_user_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Save Api Changes
				$this -> api -> saveData();
				
				// Logout user
				$appCore -> db -> update( array( 'user_id' => -1), 'sessions', 'user_id = \'' . $this -> api -> user_id . '\'');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_edited_user_ban', htmlspecialchars( $this -> api -> user_name));
				
				// Set Message
				$appCore -> output -> message( $appCore -> lang['acp_bans_user_edited'], $appCore -> lang['edit_ban_user_form']);
				
				// Hop back to Bans List
				new action_ban_users( $appCore);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_user_ban&ban=' . $this -> api -> ban_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw First Field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_user'], '<b>' . htmlspecialchars( $this-> api -> user_name) . '</b>');
		
		// Draw Lenght Group
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bans_form_sect_lenght']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_created'], $this -> appCore -> getDate( $this -> api -> ban_date, 'long'));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_expires'], ( $this -> api -> ban_lenght > 0 ? $this -> appCore -> getDate( $this -> api -> ban_lenght, 'long') : $this -> appCore -> lang['date_never']));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_lenght'], $this-> appCore -> drawList( 'ban_lenght', array(
			-1 => $this -> appCore -> lang['acp_bans_form_lenght_n'],
			0 => $this -> appCore -> lang['acp_bans_form_lenght_0'],
			'0.5' => $this -> appCore -> lang['acp_bans_form_lenght_1'],
			1 => $this -> appCore -> lang['acp_bans_form_lenght_2'],
			6 => $this -> appCore -> lang['acp_bans_form_lenght_3'],
			24 => $this -> appCore -> lang['acp_bans_form_lenght_4'],
			168 => $this -> appCore -> lang['acp_bans_form_lenght_5'],
			336 => $this -> appCore -> lang['acp_bans_form_lenght_6'],
			720 => $this -> appCore -> lang['acp_bans_form_lenght_7']
		), ( strlen( $this -> appCore -> _POST( 'ban_lenght')) > 0 ? $this -> appCore -> _POST( 'ban_lenght') : -1)));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_lenght_custom_or'], $this-> appCore -> drawTextInput( 'ban_lenght_custom', $this -> appCore -> _POST( 'ban_lenght_custom')), $this -> appCore -> lang['acp_bans_form_lenght_custom_expl']);
		
		// Draw Reasons Group
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bans_form_sect_reason']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_reason_admin'], $this-> appCore -> drawTextArea( 'ban_reason_admin', $this -> api -> ban_reason_admin));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bans_form_reason_user'], $this-> appCore -> drawTextArea( 'ban_reason_user', $this -> api -> ban_reason_user));
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['edit_ban_user_form'], array(
			$this -> appCore -> adminLink( 'act=ban_users') => $this -> appCore -> lang['acp_ban_users'],
			'' => $this -> appCore -> lang['edit_ban_user_form']
		), ob_get_clean());
			
	}

}